package com.evil.activiti.service.impl;

import com.alibaba.fastjson.JSON;
import com.evil.activiti.service.FlowConditionService;
import com.evil.activiti.util.ActivityUtil;
import com.evil.application.util.OrderDataUtil;
import com.evil.common.application.dto.judge.JudgeAnd;
import com.evil.common.application.dto.order.data.ControlDataDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程条件处理
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Component
@AllArgsConstructor
public class FlowConditionServiceImpl implements FlowConditionService {

    /**
     * 处理表单数据作为流转数据
     *
     * @param controlDataList controlDataList
     * @return Map
     */
    @Override
    public Map<String, Object> handlerControlData(List<ControlDataDTO> controlDataList) {
        return new HashMap<>(OrderDataUtil.handleFlowControlData(controlDataList, ActivityUtil::getProcessCompleteKey));
    }

    @Override
    public boolean handleCondition(String variables, String judgeAnd) {
        log.info("【SequenceFlow】进来啦，variables：{}，judgeAnd：{}", variables, judgeAnd);
        JudgeAnd and = JSON.parseObject(judgeAnd, JudgeAnd.class);
        HashMap<?, ?> variable = JSON.parseObject(variables, HashMap.class);
        String processCompleteKey = ActivityUtil.getProcessCompleteKey(and.getControlId());
        if (!variable.containsKey(processCompleteKey)) {
            return false;
        }
        List<String> key = JSON.parseArray(variable.get(processCompleteKey).toString(), String.class);

        return OrderDataUtil.handleCondition(and, key);
    }
}
